<?php
/**
 * CakeCMS后台 - 内容管理model
 * 
 * @author zzdboy
 * @version 1.0
 */
class News extends AppModel {
	public $name = 'News';
	public $useTable = 'news';
	public $primaryKey = 'id';
	
	/**
	 * 根据内容id获取内容相关信息
	 *
	 * @param int $id        	
	 */
	public function GetNewsByID($id) {
		$NewsInfo = $this->find ( 'first', array (
				'conditions' => array (
						'id' => $id 
				) 
		) );
		if ($NewsInfo) {
			if (! empty ( $NewsInfo ['News'] ['style'] )) {
				$style = explode ( ';', $NewsInfo ['News'] ['style'] );
				$NewsInfo ['News'] ['style_color'] = $style [0];
				$NewsInfo ['News'] ['style_font_weight'] = $style [1];
			} else {
				$NewsInfo ['News'] ['style_color'] = '';
				$NewsInfo ['News'] ['style_font_weight'] = '';
			}
			
			App::import ( 'Model', 'NewsData' );
			$NewsData = new NewsData ();
			$info = $NewsData->GetNewsDataByID ( $NewsInfo ['News'] ['id'] );
			$NewsInfo ['News'] ['content'] = $info ['NewsData'] ['content'];
			$NewsInfo ['News'] ['pagetype'] = $info ['NewsData'] ['pagetype'];
			$NewsInfo ['News'] ['maxcharperpage'] = $info ['NewsData'] ['maxcharperpage'];
			$NewsInfo ['News'] ['relation'] = $info ['NewsData'] ['relation'];
			$NewsInfo ['News'] ['allow_comment'] = $info ['NewsData'] ['allow_comment'];
			$NewsInfo ['News'] ['copyfrom'] = $info ['NewsData'] ['copyfrom'];
		}
		return $NewsInfo;
	}
	
	/**
	 * 添加内容
	 *
	 * @param array $data        	
	 */
	public function add($data) {
		if (! is_array ( $data )) {
			return false;
		}
		// 如果在一次插入完成后立即进行下一次插入， 那么第二次插入的内容完全不会被插入，而是会被更新到第一次插入的行中
		// 正确的做法是调用create()初始化model，再set/save新数据。
		$this->create ();
		if ($this->save ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 编辑内容
	 *
	 * @param array $data        	
	 */
	public function edit($data) {
		if (! is_array ( $data )) {
			return false;
		}
		
		if ($this->save ( $data )) {
			return true;
		}
		return false;
	}
	public function del($id) {
		if ($this->delete ( $id )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 取得内容列表
	 *
	 * @param array $conditions
	 *        	查询条件
	 * @param int $limit
	 *        	记录数
	 */
	public function GetNewsByList($conditions = array(), $limit = 10) {
		$result = array ();
		
		$result ['result'] = $this->find ( 'all', array (
				'conditions' => $conditions,
				'limit' => $limit,
				'order' => 'id DESC' 
		) );
		
		$result ['count'] = $this->find ( 'count', array (
				'conditions' => $conditions 
		) );
		
		return $result;
	}
}
?>